using MapMaker.VirtualEarth;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace MapMaker
{
    public partial class frmSelectTiles : Form
    {
        Queue imageList;
        
        public frmSelectTiles(Queue imageList)
        {
            InitializeComponent();

            this.imageList = imageList;
        }

        private void loadTiles()
        {
            if (((frmDownloadOptions)this.Owner).PreviewLoaded)
            {
                foreach (PictureBox image in imageList)
                {
                    this.Controls.Add(image);
                }
            }
            else
            {
                foreach (PictureBox image in imageList)
                {
                    image.Click += new EventHandler(image_Click);
                    this.Controls.Add(image);
                }

                ((frmDownloadOptions)this.Owner).PreviewLoaded = true;
            }
        }

        private void image_Click(object sender, EventArgs e)
        {
            PictureBox tileImage = ((PictureBox)sender);
            Tile tileData = (Tile)tileImage.Tag;

            if (Program.TileAccess.TilesToDownload.Contains(tileData))
            {
                tileImage.SizeMode = PictureBoxSizeMode.StretchImage;
                tileImage.Image = new Bitmap(Path.Combine(Application.StartupPath, "images\\gray.bmp"));
                Program.TileAccess.TilesToDownload.Remove(tileData);
            }
            else
            {
                tileImage.Image = null;
                tileImage.Update();
                Program.TileAccess.TilesToDownload.Add(tileData);
            }
        }

        private void frmSelectTiles_Load(object sender, EventArgs e)
        {
            loadTiles();
        }
    }
}